<?php

    class UserController extends Controller {
        public $metaDescription = null;
        public $metaKeywords = null;
        public $linkCanoncical = null;
        public $title = null;
        public $layout = 'column_main';

        public function actionLogout(){
            $url = new Url();
            session_unset();
            //$this->redirect($_SERVER["HTTP_REFERER"]);
            $this->redirect($url->createUrl("site/index"));
        }

        public function actionIndex(){
            $url = new Url();
            $this->title = "Trang ca nhan | xem ket qua xo so truc tiep | ket qua ve so truc tuyen";

            //$_SESSION["user"] = array("id"=>4,"username"=>"nvha9589");
            if(isset($_SESSION["user_edu_net"])){ 
                $id_user = intval($_SESSION["user_edu_net"]["user_id"]);
                $data = User::getRowById($id_user); 

                $post_data = PostUser::getPostsByReceiverID($id_user);

                //$post_reply = ReplyUser::getReplysByReceiverID($id_user);
                $post_reply = ReplyUser::getAllRows();

                $data_user = User::getAllRows();

                /*echo "<pre>";
                print_r($post_reply);
                echo "</pre>";die;*/


                $this->render("index",array("data"=>$data,"data_user"=>$data_user,"post_data"=>$post_data,"post_reply"=>$post_reply));
            }else{
                $this->redirect($url->createUrl("user/login")); 
            }
        }

        public function actionLogin(){
            $url = new Url();
            $this->layout = "column_login";
            if(!isset($_SESSION["user_edu_net"])){
                $this->render("login",array());
            }else{
                $this->redirect($url->createUrl("user/index"));
            }
        }        

        public function actionRegister(){
            $url = new Url();
            $this->layout = "column_register";
            if(!isset($_SESSION["user_edu_net"])){
                $this->render("register",array());
            }else{
                echo "<script type='text/javascript'>history.go(-1);</script>";
            }

        }

        public function actionDetail(){
            $url = new Url();
            $this->render("detail",array());
        }

        public function actionChangePass(){
            $url = new Url();
            if(isset($_SESSION["user_pm"])){  
                $id_user = intval($_SESSION["user_pm"]["user_id"]);
                $data = User::getRowById($id_user);                             
                $this->render("change_pass",array("data"=>$data));
            }else{
                $this->redirect($url->createUrl("site/index"));
            }

        }        

        public function actionAjaxSubmitLogin(){

            $email = isset($_POST["email"])?mysql_escape_string($_POST["email"]):"";
            $pass = isset($_POST["password"])?mysql_escape_string($_POST["password"]):"";

            $password = User::getMd5($pass);
            $row = User::getRowByEmailAndPass($email,$pass);
            if($row){  
                $rowUser = User::getRowById($row["user_id"]);                 
                $_SESSION["user_edu_net"]=$rowUser;                
                echo 1;
            }else{
                echo 0;
            }

        }    

        public function actionAjaxSubmitRegister(){            

            $email = isset($_GET["email"])?mysql_escape_string($_GET["email"]):"";            
            $password = isset($_GET["password"])?mysql_escape_string($_GET["password"]):"";
            $full_name = isset($_GET["full_name"])?mysql_escape_string($_GET["full_name"]):"";            
            $phone_number = isset($_GET["phone_number"])?mysql_escape_string($_GET["phone_number"]):"";            
            $group_code = isset($_GET["group_code"])?mysql_escape_string($_GET["group_code"]):"";            
            $user_type = isset($_GET["user_type"])?mysql_escape_string($_GET["user_type"]):0;

            $rowUser = User::getRowByEmail($email);

            if($rowUser){
                echo -3;exit;
            }

            if($user_type != 1){
                $rowGroup = Group::getRowByGroupCode($group_code);

                if(!$rowGroup){
                    echo -2;exit;
                }   
            }            

            $password = User::getMd5($password);

            $array_input['user_email'] = $email;
            $array_input['user_password'] = $password;
            $array_input['user_fullname'] = $full_name;
            $array_input['user_mobile'] = $phone_number;
            $array_input['user_type'] = $user_type;
            $array_input['user_create_date'] = time();
            $last_id = User::insertObject($array_input);
            if($last_id !=0){

                $array_input_2['group_id'] = $rowGroup['group_id'];
                $array_input_2['user_id'] = $last_id;
                $array_input_2['member_type'] = 0;
                $array_input_2['member_status'] = 1;
                $array_input_2['member_create_date'] = time();
                $row = Member::insertObject($array_input_2);

                $rowUser = User::getRowById($last_id);    
                //$_SESSION["user"] = $rowUser;        
                echo 1;
            }else{
                echo 0;
            }
        }

        public function actionAjaxSubmitUpdate(){            
            $user_id = isset($_GET["user_id"])?mysql_escape_string($_GET["user_id"]):"";
            $fullname = isset($_GET["fullname"])?mysql_escape_string($_GET["fullname"]):"";
            $birthday = isset($_GET["birthday"])?mysql_escape_string($_GET["birthday"]):"";
            $email = isset($_GET["email"])?mysql_escape_string($_GET["email"]):"";                                                        
            $array_input['fullname'] = $fullname;
            $array_input['birthday'] = strtotime(DateUtils::convertDatedb($birthday));
            $array_input['email'] = $email;
            $last_id = User::updateObject($array_input,"user_id",$user_id);
            if($last_id !=0){            
                echo 1;
            }else{
                echo 0;
            }
        }

        public function actionAjaxSubmitChangePass(){            
            $user_id = isset($_GET["user_id"])?mysql_escape_string($_GET["user_id"]):"";
            $password = isset($_GET["password"])?mysql_escape_string($_GET["password"]):"";
            $new_password = isset($_GET["new_password"])?mysql_escape_string($_GET["new_password"]):"";            

            $row = User::getRowById($user_id);

            $password = User::getMd5($password);                        

            if($password != $row['password']){
                echo -3;exit;
            }            

            $array_input['password'] = User::getMd5($new_password);            
            $last_id = User::updateObject($array_input,"user_id",$user_id);

            if($last_id !=0){            
                echo 1;
            }else{
                echo 0;
            }
        }

        public function actionAjaxSendComment(){            

            $stt_content = isset($_GET["stt_content"])?mysql_escape_string($_GET["stt_content"]):"";            
            $receiver_id = isset($_GET["receiver_id"])?mysql_escape_string($_GET["receiver_id"]):"";
            $receiver_name = isset($_GET["receiver_name"])?mysql_escape_string($_GET["receiver_name"]):"";
            $user_id = isset($_GET["user_id"])?mysql_escape_string($_GET["user_id"]):"";            

            /*$rowUser = User::checkUserByName($receiver_name);

            if(!$rowUser){
            echo -3;exit;
            }*/

            $array_input['pUser_sender_id'] = $user_id;
            //$array_input['pUser_receiver_id'] = $rowUser['user_id'];
            $array_input['pUser_receiver_id'] = $receiver_id;
            $array_input['pUser_content'] = $stt_content;            
            $array_input['pUser_create_date'] = time();
            $last_id = PostUser::insertObject($array_input);
            if($last_id !=0){            
                echo 1;
            }else{
                echo 0;
            }
        }        

        public function actionAjaxSendReply(){

            $post_id = isset($_GET["post_id"])?mysql_escape_string($_GET["post_id"]):"";            
            $reply_content = isset($_GET["reply_content"])?mysql_escape_string($_GET["reply_content"]):"";
            $user_id = isset($_GET["user_id"])?mysql_escape_string($_GET["user_id"]):"";            

            /*$rowUser = User::checkUserByName($receiver_name);

            if(!$rowUser){
            echo -3;exit;
            }*/

            $array_input['rUser_post_id'] = $post_id;
            $array_input['rUser_sender'] = $user_id;
            $array_input['rUser_receiver'] = $user_id;            
            $array_input['rUser_content'] = $reply_content;            
            $array_input['rUser_create_date'] = time();
            $last_id = ReplyUser::insertObject($array_input);
            if($last_id !=0){            
                echo 1;
            }else{
                echo 0;
            }
        }        

    }

?>
